package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.UserLoginInfoMapper;
import com.danciyixue.boot.dcyx.service.UserLoginInfoService;
import com.danciyixue.boot.dcyx.model.entity.UserLoginInfo;
import com.danciyixue.boot.dcyx.model.form.UserLoginInfoForm;
import com.danciyixue.boot.dcyx.model.query.UserLoginInfoQuery;
import com.danciyixue.boot.dcyx.model.vo.UserLoginInfoVO;
import com.danciyixue.boot.dcyx.converter.UserLoginInfoConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 用户登录验证信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:37
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class UserLoginInfoServiceImpl extends ServiceImpl<UserLoginInfoMapper, UserLoginInfo> implements UserLoginInfoService {

    private final UserLoginInfoConverter userLoginInfoConverter;

    /**
    * 获取用户登录验证信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserLoginInfoVO>} 用户登录验证信息分页列表
    */
    @Override
    public IPage<UserLoginInfoVO> getUserLoginInfoPage(UserLoginInfoQuery queryParams) {
        Page<UserLoginInfoVO> pageVO = this.baseMapper.getUserLoginInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户登录验证信息表单数据
     *
     * @param id 用户登录验证信息ID
     * @return 用户登录验证信息表单数据
     */
    @Override
    public UserLoginInfoForm getUserLoginInfoFormData(Long id) {
        UserLoginInfo entity = this.getById(id);
        return userLoginInfoConverter.toForm(entity);
    }
    
    /**
     * 新增用户登录验证信息
     *
     * @param formData 用户登录验证信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveUserLoginInfo(UserLoginInfoForm formData) {
        UserLoginInfo entity = userLoginInfoConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新用户登录验证信息
     *
     * @param id   用户登录验证信息ID
     * @param formData 用户登录验证信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateUserLoginInfo(Long id,UserLoginInfoForm formData) {
        UserLoginInfo entity = userLoginInfoConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户登录验证信息
     *
     * @param ids 用户登录验证信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteUserLoginInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户登录验证信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
